﻿if ($.Board == null)
	$.Board = {};
$.Board.getList = function()
{
	$.ajaxNet(
		"/manager/board/boardmethod.aspx?_vb=GetListBoard",
		{
			PageNo : $("div.pager").pager("option", "currentPageNo"),
			PageSize : $("div.pager").pager("option", "pageSize")
		}, function(results, context)
		{
			$("table.managerList tbody").html(results[0]);
			$("div.pager").pager("update", results[1]);
		});
};

$.Board.showInputForm = function(boardEntity) {
	$("div.inputForm").find("td.boardJsonDataArea span").remove();
	if (typeof(boardEntity) == "undefined")
	{
		$("div.inputForm")
			.find("input.boardSN").val("0").removeAttr("disabled").end()
			.find("input.boardName").val("").end()
			.show();
	}
	else
	{
	    if (boardEntity.strJsonData != "")
	    {
	        var addtionalDatas = JSON.parse(boardEntity.strJsonData);
	        
	        $.each(addtionalDatas, function(idx, dataName) {
                $("div.inputForm").find("td.boardJsonDataArea").append($("<span><input type='text' class='boardJsonData' style='width:200px' value='"+dataName+"'/><a href='#' onclick='$.Board.RemoveJsonDataForm(this);return false;'>X</a><br/></span>"));
	        });
	    }

		$("div.inputForm")
			.find("input.boardSN").val(boardEntity.n1BoardSN).attr("disabled", "disabled").end()
			.find("input.boardName").val(boardEntity.strBoardName).end()
			.show();
	}
}

$.Board.AddJsonDataForm = function() {
    $("div.inputForm").find("td.boardJsonDataArea").append($("<span><input type='text' class='boardJsonData' style='width:200px' value=''/><a href='#' onclick='$.Board.RemoveJsonDataForm(this);return false;'>X</a><br/></span>"));
}

$.Board.RemoveJsonDataForm = function(elem) {
	$(elem).parent().remove();
}

$.Board.cancelInputForm = function() {
	$("div.inputForm").hide();
}

$.Board.submitInputForm = function() {
	var n1BoardSN = $("div.inputForm input.boardSN").val();
	var strBoardName = $("div.inputForm input.boardName").val();

	var jsonDataNames = new Array();
	$("div.inputForm td.boardJsonDataArea span input").each(function(idx, elem) {
		if ($(elem).val().trim() != "")
			jsonDataNames.push($(elem).val());
	});

	var strBoardJsonData = JSON.stringify(jsonDataNames);

	if ($("div.inputForm input.boardSN").attr("disabled") == "disabled")
	{
		$("div.inputForm").ajaxNet(
			"/manager/board/boardmethod.aspx?_vb=ModifyBoard",
			{
				"BoardSN" : n1BoardSN,
				"BoardName" : strBoardName,
				"JsonData" : strBoardJsonData
			}, function(results, context)
			{
				$("div.inputForm").hide();
				alert('수정되었습니다.');
				$.Board.getList();
			});
	}
	else
	{
		$("div.inputForm").ajaxNet(
			"/manager/board/boardmethod.aspx?_vb=CreateBoard",
			{
				"BoardSN" : n1BoardSN,
				"BoardName" : strBoardName,
				"JsonData" : strBoardJsonData
			}, function(results, context)
			{
				$("div.inputForm").hide();
				alert('추가되었습니다.');
				$("div.pager").pager("pageNo", 1);
				$.Board.getList();
			});
	}
}

$.Board.modify = function(elem) {
	var boardEntity = $(elem).closest("tr").tagdata();
	
	$.Board.showInputForm(boardEntity);
};

$.Board.remove = function(elem) {
	var manageEntity = $(elem).closest("tr").tagdata();

	$("table.managerList").ajaxNet(
		"/manager/board/boardmethod.aspx?_vb=RemoveBoard",
		{
			"BoardSN" : manageEntity.n1BoardSN
		}, function(results, context)
		{
			alert('삭제되었습니다.');
			$.Board.getList();
			
		});
}